﻿package mortal.game.view.rebate.shop.panel
{
    import Message.Game.*;
    import com.mui.controls.*;
    import mortal.game.cache.*;
    import mortal.game.view.common.*;
    import mortal.game.view.rebate.data.*;
    import mortal.game.view.rebate.shop.item.*;

    public class RebateShopButtom extends GSprite
    {
        protected var _bigCell:RebatShopBigCell;
        protected var _leftTop:CellsGridSprite;
        protected var _buttomList:CellsGridSprite;
        private var _cShopId:int = -1;
        private var _cellObj:Object;

        public function RebateShopButtom()
        {
            this._cellObj = {};
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bigCell = UIFactory.getUICompoment(RebatShopBigCell, 0, 0, this);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this.disposeAllCells(param1);
            this._cShopId = -1;
            return;
        }// end function

        private function disposeAllCells(param1:Boolean = true) : void
        {
            this._cellObj = {};
            this._bigCell.dispose(param1);
            this._bigCell = null;
            if (this._leftTop)
            {
                this._leftTop.dispose(param1);
                this._leftTop = null;
            }
            if (this._buttomList)
            {
                this._buttomList.dispose(param1);
                this._buttomList = null;
            }
            return;
        }// end function

        private function genLeftTop() : void
        {
            if (this._leftTop)
            {
                return;
            }
            this._leftTop = UIFactory.getUICompoment(CellsGridSprite);
            this._leftTop.colNum = 2;
            this._leftTop.vCellWid = 215;
            this._leftTop.hCellWid = 157;
            this._leftTop.genCellFun = UIFactory.getUICompoment;
            this._leftTop.x = 239;
            this._leftTop.y = 0;
            this.addChild(this._leftTop);
            return;
        }// end function

        private function genButton() : void
        {
            if (this._buttomList)
            {
                return;
            }
            this._buttomList = UIFactory.getUICompoment(CellsGridSprite);
            this._buttomList.colNum = 3;
            this._buttomList.vCellWid = 227;
            this._buttomList.hCellWid = 157;
            this._buttomList.genCellFun = UIFactory.getUICompoment;
            this._buttomList.x = 0;
            this._buttomList.y = 318;
            this.addChild(this._buttomList);
            return;
        }// end function

        private function initCells() : void
        {
            var _loc_3:RebateShopCell = null;
            var _loc_5:Object = null;
            if (this._leftTop != null)
            {
                return;
            }
            var _loc_1:* = Cache.instance.rebate.curShopId;
            var _loc_2:* = RebateConfig.instance().itemShopObj[_loc_1];
            if (_loc_2 == null)
            {
                return;
            }
            this.genLeftTop();
            if (_loc_2.length < 5)
            {
                this._leftTop.genCells((_loc_2.length - 1), RebateShopCell);
            }
            else
            {
                this._leftTop.genCells(4, RebateShopCell);
                this.genButton();
                this._buttomList.genCells(_loc_2.length - 5, RebateShopCell);
            }
            var _loc_4:int = 0;
            while (_loc_4 < _loc_2.length)
            {
                
                _loc_5 = _loc_2[_loc_4];
                if (_loc_4 == 0)
                {
                    this._bigCell.updateBaseInfo(_loc_5);
                    this._cellObj[_loc_5.code + "_" + _loc_5.itemCode] = this._bigCell;
                }
                else if (_loc_4 >= 1 && _loc_4 <= 4)
                {
                    _loc_3 = this._leftTop.cellArr[(_loc_4 - 1)];
                    if (_loc_3)
                    {
                        _loc_3.updateBaseInfo(_loc_5);
                        this._cellObj[_loc_5.code + "_" + _loc_5.itemCode] = _loc_3;
                    }
                }
                else
                {
                    _loc_3 = this._buttomList.cellArr[_loc_4 - 5];
                    if (_loc_3)
                    {
                        _loc_3.updateBaseInfo(_loc_5);
                        this._cellObj[_loc_5.code + "_" + _loc_5.itemCode] = _loc_3;
                    }
                }
                _loc_4++;
            }
            return;
        }// end function

        public function updateItemNum(param1:Array) : void
        {
            var _loc_3:RebateShopCell = null;
            var _loc_5:SOnSellItem = null;
            if (this.isDisposed)
            {
                return;
            }
            var _loc_2:* = Cache.instance.rebate.curShopId;
            if (this._cShopId != -1 && this._cShopId != _loc_2)
            {
                this.disposeAllCells();
                this._bigCell = UIFactory.getUICompoment(RebatShopBigCell, 0, 0, this);
            }
            this._cShopId = _loc_2;
            if (this._leftTop == null)
            {
                this.initCells();
            }
            var _loc_4:int = 0;
            while (_loc_4 < param1.length)
            {
                
                _loc_5 = param1[_loc_4];
                _loc_3 = this._cellObj[_loc_2 + "_" + _loc_5.itemCode];
                if (_loc_3)
                {
                    _loc_3.updateCellRemain(_loc_5);
                }
                _loc_4++;
            }
            return;
        }// end function

        public function updatePlayerRecord(param1:Array) : void
        {
            var _loc_3:RebateShopCell = null;
            var _loc_5:SOnSellItemPlayer = null;
            if (this.isDisposed)
            {
                return;
            }
            var _loc_2:* = Cache.instance.rebate.curShopId;
            var _loc_4:int = 0;
            while (_loc_4 < param1.length)
            {
                
                _loc_5 = param1[_loc_4];
                _loc_3 = this._cellObj[_loc_2 + "_" + _loc_5.itemCode];
                if (_loc_3)
                {
                    _loc_3.updatePlayerInfo(_loc_5);
                }
                _loc_4++;
            }
            return;
        }// end function

        public function initData() : void
        {
            var _loc_2:RebateShopCell = null;
            var _loc_4:SOnSellItemPlayer = null;
            if (this.isDisposed)
            {
                return;
            }
            if (this._leftTop == null)
            {
                this.initCells();
            }
            var _loc_1:* = Cache.instance.rebate.curShopId;
            var _loc_3:* = Cache.instance.rebate.playerInfo;
            for each (_loc_4 in _loc_3)
            {
                
                if (_loc_4 == null)
                {
                    continue;
                }
                _loc_2 = this._cellObj[_loc_1 + "_" + _loc_4.itemCode];
                if (_loc_2)
                {
                    _loc_2.updatePlayerInfo(_loc_4);
                }
            }
            return;
        }// end function

    }
}
